﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPF_experement
{
    static class Trevor_A_star
    {
        static Travor_WorldState root;
        public static bool diagonal;
        public static BinaryHeap<Travor_WorldState> openList;
        public static HashSet<Travor_WorldState> closedList;
        public static HashSet<Travor_WorldState> closedListExpanded;

        static public void init_Travor_A_star(AgentState[] allAgentsState, bool i_diagonal)
        {
            diagonal = i_diagonal;
            closedList = new HashSet<Travor_WorldState>();
            closedListExpanded = new HashSet<Travor_WorldState>();
            root = new Travor_WorldState(allAgentsState);
            openList = new BinaryHeap<Travor_WorldState>();
            closedList.Add(root);
            openList.Add(root);
        }

        public static Travor_WorldState solve()
        {
            Travor_WorldState temp;
            while (openList.Count > 0)
            {
                if (Run.watch.ElapsedMilliseconds > 300000)
                {
                    Trevor.totalTime = -2;
                    free();
                    return null;
                }
                temp = openList.Remove();
                if (temp.goalTest())
                {
                    free();
                    return temp;
                }
                if (!closedListExpanded.Contains(temp))
                {
                    closedListExpanded.Add(temp);
                    Trevor.expanded++;
                    temp.expandNode();
                }
            }
            Trevor.totalTime = -1;
            free();
            return null;
        }
        private static void free()
        {
            openList = null;
            closedList = null;
            closedListExpanded = null;
        }
        private static void printSolution(Travor_WorldState end)
        {
            int step = 0;
            Console.WriteLine("solution back to front");
            while (end != null)
            {
                Console.WriteLine("step " + step);
                Console.WriteLine(end.ToString());
                step++;
                end = end.prevStep;
            }
        }
    }
}



//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;

//namespace CPF_experement
//{
//    static class Trevor_A_star
//    {
//        static Travor_WorldState root;
//        public static bool diagonal;
//        public static BinaryHeap<Travor_WorldState> openList;
//        public static HashSet<Travor_WorldState> closedList;

//        static public void init_Travor_A_star(AgentState[] allAgentsState, bool i_diagonal)
//        {
//            diagonal = i_diagonal;
//            closedList = new HashSet<Travor_WorldState>();
//            root = new Travor_WorldState(allAgentsState);
//            openList = new BinaryHeap<Travor_WorldState>();
//            closedList.Add(root);
//            openList.Add(root);
//        }

//        public static bool solve()
//        {
//            Travor_WorldState temp;
//            while (openList.Count > 0)
//            {
//                temp = openList.Remove();
//                if (temp.goalTest())
//                {
//                    printSolution(temp);
//                    return true;
//                }
//                temp.expandNode();
//            }
//            return false;
//        }
//        private static void printSolution(Travor_WorldState end)
//        {
//            int step = 0;
//            Console.WriteLine("solution back to front");
//            while (end != null)
//            {
//                Console.WriteLine("step " + step);
//                Console.WriteLine(end.ToString());
//                step++;
//                end = end.prevStep;
//            }
//        }
//    }
//}
